//
//  BQBuyCarViewController.m
//  BeeQuick
//
//  Created by mac on 16/11/18.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "BQBuyCarViewController.h"
#import "CZAdditions.h"
#import "BQShopProductCell.h"
#import "BQShopCarTopCell.h"
#import "BQBottomCarView.h"
#import "BQSubmitOrderController.h"
#import "BQMyCarTool.h"
static NSString * topCell = @"topCell";
static NSString * shopSecondCell = @"shopSecondCell";
static NSString * thirdCell = @"shopThirdCell";
static NSString * fourCell = @"shopFourCell";
static NSString * threeImgCell = @"threeImgCell";
static NSString *productCell = @"productCell";
@interface BQBuyCarViewController ()<UITableViewDelegate,UITableViewDataSource,PushToSumbitOrderDelegate,BQShopProductCellDelegate>

@property(nonatomic,weak)UIView *bottomView;

@property(weak,nonatomic)UITableView *carTableView;


@property(nonatomic,strong)UIView *backgroundView;


@end

@implementation BQBuyCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//    if(模型为空)
//    {
//          [self setupEmptyUI];
//    }
//    else
//    {
//    [self setupMainUI];
//    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshMyList:) name:@"refresh" object:nil];
    
}
- (void)refreshMyList:(NSNotification*)no
{
    [self.carTableView reloadData];
    
}


//界面显示的时候刷新一下 tableview
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.carTableView reloadData];

    
    
    //MARK: - 对购物车单例进行判断.
    
    if(myCar.goodArrM.count != 0)
    {
//        [self.backgroundView removeFromSuperview];
//        //如果有值
//        self.backgroundView = [[UIView alloc]init];
//        
//        [self.view addSubview:self.backgroundView];
//        
//        self.backgroundView.frame = self.view.frame;
        
        [self creatBackgroundView];
        
        [self setupMainUI];
        
    }
    else
    {
//        [self.backgroundView removeFromSuperview];
//        
//        self.backgroundView = [[UIView alloc]init];
//        
//        [self.view addSubview:self.backgroundView];
//        
//        self.backgroundView.frame = self.view.frame;
        
        [self creatBackgroundView];
        //没值
        [self setupEmptyUI];
    }

    
    
    
    
    
    
    
    
}




#pragma mark - tableView的数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 1;
    }
//    return 4;//假数据, 等后面的model数据--这里最少也要 写3
    
    return 3 + [BQMyCarTool shareMyCar].goodArrM.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if(indexPath.section == 0)
    {
        
        BQShopCarTopCell *cell = [tableView dequeueReusableCellWithIdentifier:topCell forIndexPath:indexPath];
        return cell;
        
    }
    if(indexPath.section == 1 && indexPath.row == 0)
    {
        BQShopCarTopCell *cell = [tableView dequeueReusableCellWithIdentifier:shopSecondCell forIndexPath:indexPath];
        
        return cell;
    }

    if(indexPath.section == 1 && indexPath.row == 1)
    {

    BQShopCarTopCell *cell = [tableView dequeueReusableCellWithIdentifier:thirdCell forIndexPath:indexPath];
    return cell;
    }
    
    if(indexPath.section == 1 && indexPath.row == 2)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:threeImgCell forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    BQShopProductCell *cell = [tableView dequeueReusableCellWithIdentifier:productCell forIndexPath:indexPath];
    
    cell.delegate = self;
    
    cell.model = myCar.goodArrM[indexPath.row-3];
    
    return cell;
}

#pragma mark - 搭建UI界面
- (void)setupEmptyUI
{
    
    UIView *backgroundView = [[UIView alloc]init];
    
    [self.view addSubview:backgroundView];
    
    //关联
    self.backgroundView = backgroundView;
    
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.navigationItem.title = @"购物车";

    
    UIImageView * midImg = [[UIImageView alloc]init];
    self.backgroundView.backgroundColor = [UIColor cz_colorWithHex:0xefefef];
    
    [self.backgroundView addSubview:midImg];
    
    midImg.image = [UIImage imageNamed:@"v2_shop_empty"];
    
    [midImg sizeToFit];
    
    [midImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backgroundView);
        make.centerY.equalTo(self.backgroundView).offset(-50);
    }];

    
    
    
    UILabel *label = [[UILabel alloc]init];
    
    label.text = @"亲,购物车空空的耶~赶紧的去挑好吃的把";
//    [label sizeToFit];
    [self.backgroundView addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backgroundView);
        make.top.equalTo(midImg.mas_bottom).offset(30);
    }];
    
    label.textColor = [UIColor cz_colorWithHex:0x878383];
    
    
    UIButton * btn = [[UIButton alloc]init];
    [btn setTitle:@"去逛逛吧" forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor cz_colorWithHex:0x646464] forState:UIControlStateNormal];
    
    [self.backgroundView addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backgroundView);
        make.top.equalTo(label.mas_bottom).offset(20);
        make.size.mas_equalTo(CGSizeMake(180, 30));
    }];
    
//给按钮切割圆角
    btn.layer.cornerRadius = 10;
    
//    btn.titleLabel.textColor = [UIColor cz_colorWithHex:0x646464];
//    btn.tintColor = [UIColor cz_colorWithHex:0x646464];
    
    //MARK: - 给按钮一个点击事件.
    [btn addTarget:self action:@selector(goShoping:) forControlEvents:UIControlEventTouchUpInside];
    
    btn.layer.borderWidth = 1.0;
    btn.layer.borderColor = [UIColor cz_colorWithHex:0x646464].CGColor;//记得转成C的

    
  
}


- (void)setupMainUI
{
    
    
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    
    
    
    
    //MARK: - 关联.
    
    self.carTableView = tableView;
    
    [self.backgroundView addSubview:tableView];
    
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.backgroundView);
        make.top.equalTo(self.backgroundView).offset(64);
        make.bottom.equalTo(self.backgroundView).offset(-49);
    }];
    
    //MARK: - 有货物的购物车的设置数据源.
    tableView.delegate = self;
    tableView.dataSource = self;
    
    
    UIView *bottomView = [[UIView alloc]init];
    [self.view addSubview:bottomView];
    self.view.backgroundColor = [UIColor yellowColor];
    
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.backgroundView);
        make.height.mas_equalTo(64);
    }];

    
    
    
    //MARK: - 注册.

    [tableView registerNib:[UINib nibWithNibName:@"BQShopCarTopCell" bundle:nil] forCellReuseIdentifier:topCell];
    
    
    [tableView registerNib:[UINib nibWithNibName:@"BQShopSecondCell" bundle:nil] forCellReuseIdentifier:shopSecondCell];

    
    [tableView registerNib:[UINib nibWithNibName:@"BQShopThirdCell" bundle:nil] forCellReuseIdentifier:thirdCell];
    
    [tableView registerNib:[UINib nibWithNibName:@"BQShopFourCell" bundle:nil] forCellReuseIdentifier:fourCell];
    

    [tableView registerNib:[UINib nibWithNibName:@"BQShopProductCell" bundle:nil] forCellReuseIdentifier:productCell];
    
    
    [tableView registerNib:[UINib nibWithNibName:@"BQThreeImgCell" bundle:nil] forCellReuseIdentifier:threeImgCell];
    
    //MARK: - 创建他的底部视图.
//
    [tableView.tableFooterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kScreenW, 100));
    }];

    BQBottomCarView *bottomCarView = [[[UINib nibWithNibName:@"BQBottomCarView" bundle:nil]instantiateWithOwner:nil options:0]lastObject];
    
    
    [tableView setTableFooterView:bottomCarView];
    
    //MARK: -bottomView代理设置
    bottomCarView.delegate = self;
    
    
    
//    tableView.tableFooterView = redView;
//    redView.frame = CGRectMake(0, 0, kScreenW, 100);
    
    
    tableView.backgroundColor = [UIColor cz_colorWithHex:0xefefef];

    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if(indexPath.section == 0)
//    {
//        return 85.0;
//    }
//    else if (indexPath.row == 0)
//    {
//        return 50;
//    }
//    else if (indexPath.row == 1)
//    {
//        return 70;
//    }
//    else
//    {
//        return 70;
//    }
    if(indexPath.section == 0)
    {
        return 90.0;
    }
    else if (indexPath.row == 0)
    {
        return 50;
    }
    else if (indexPath.row == 1)
    {
        return 80;
    }
    else
    {
        return 80;
    }


}

//
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 10;
    }
    if(section == 1)
    {
        return 1;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 10;
    }
    return 1;
}


#pragma mark - 去逛逛按钮的点击事件
- (void)goShoping:(UIButton *)btn
{
    NSLog(@"我准备去逛一逛");
    //跳转到首页 -- 使用通知
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"goShoping" object:nil];
    
    
}
#pragma mark - 选好了按钮的点击事件
-(void)PushToSumbitOrder:(UIButton *)button{
    BQSubmitOrderController * soVc = [[BQSubmitOrderController alloc]init];
    
    [self.navigationController pushViewController:soVc animated:YES];
    
    
}



#pragma mark - 创建背景图

- (void)creatBackgroundView
{
    [self.backgroundView removeFromSuperview];
    
    self.backgroundView = [[UIView alloc]init];
    
    [self.view addSubview:self.backgroundView];
    
    self.backgroundView.frame = self.view.frame;
    
}



#pragma mark - 实现productCell的代理方法

- (void)deleteGoodsWithCell:(BQShopProductCell *)cell andModel:(BQHomeGoodModel *)model
{
    //MARK: - 弹出一个提示框.
    
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"删除商品" message:@"确认删除此商品吗?" preferredStyle:UIAlertControllerStyleAlert];
    
    // Create the actions.
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
        
        
        //MARK: - 这里如果点取消了 就要再给他加1回来.
        
        [myCar addGoods:model];
        
        
        [self.carTableView reloadData];
        
        
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"确认");
        
        //MARK: - 如果点击确认.  再次刷新一下tableview
        [self.carTableView reloadData];
        
        //这里要判断一下
        if(myCar.goodArrM.count == 0)
        {
            //表示没有商品了.切换到另一个界面
            
            [self creatBackgroundView];
            [self setupEmptyUI];
        }
        
        
    }];
    
    // Add the actions.
    [alertVc addAction:cancelAction];
    [alertVc addAction:otherAction];
    

    [self presentViewController:alertVc animated:YES completion:nil];
    
    
    
    
}
@end
